package cn.hairui.fastms.base;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * <p>
 * <h2>请添加简述</h2>
 *   <ol>请添加详细描述</ol>
 * <h2>修改历史</h2>
 *   <ol>如有修改，请添加修改历史</ol>
 * </p>
 *
 * @author spdb/lihr3
 * @date 2020/11/28 23:35
 * @copyright spdb
 */
public class CodeGenerator {
    // 1数据源配置
    public static DataSourceConfig dataSourceConfig() {
        DataSourceConfig dataSource = new DataSourceConfig();
        dataSource.setDbType(DbType.MYSQL);
        dataSource.setDriverName("com.mysql.jdbc.Driver");
        dataSource.setUsername("root");
        dataSource.setPassword("111111");
        dataSource.setUrl("jdbc:mysql://localhost:3306/fastms?useUnicode=true&characterEncoding=utf8&useSSL=false");
        return dataSource;
    }

    // 2全局配置
    public static GlobalConfig globalConfig() {
        GlobalConfig globalConfig = new GlobalConfig();
        //项目根目录
        String projectPath = System.getProperty("user.dir");
        System.out.println(projectPath);
        //用于多个模块下生成到精确的目录下（我设置在桌面）
        //代码生成目录
        globalConfig.setOutputDir(projectPath + "/src/main/java");

        globalConfig.setOpen(false);// 是否打开输出目录(默认值：null)

        globalConfig.setSwagger2(true);//实体属性 Swagger2 注解
        globalConfig.setFileOverride(true);// 是否覆盖已有文件(默认值：false)
        globalConfig.setActiveRecord(false);// 不需要ActiveRecord特性的请改为false
        globalConfig.setEnableCache(false);// XML 二级缓存
        globalConfig.setBaseResultMap(true);// XML ResultMap
        globalConfig.setBaseColumnList(true);// XML columList
        globalConfig.setAuthor("lihairui");// 作者

        // 自定义文件命名，注意 %s 会自动填充表实体属性！
        globalConfig.setControllerName("%sController");
        globalConfig.setServiceName("%sService");
        globalConfig.setServiceImplName("%sServiceImpl");
        globalConfig.setMapperName("%sMapper");
        globalConfig.setXmlName("%s.mapper");

        return globalConfig;
    }

    // 3包配置
    public static PackageConfig packageConfig() {

        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("cn.hairui.fastms.core");// 父包名。如果为空，将下面子包名必须写全部， 否则就只需写子包名
        //packageConfig.setController("controller");
        packageConfig.setService("service");
        packageConfig.setServiceImpl("service");
        packageConfig.setMapper("mapper");
        packageConfig.setEntity("bean");
        //packageConfig.setXml("mapper");
        return packageConfig;

    }

    // 4策略配置
    public static StrategyConfig strategyConfig() {

        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setInclude("s_msg"); // 需要生成的表

        //strategyConfig.setSuperControllerClass("");
        //strategyConfig.setSuperServiceClass("");
        //strategyConfig.setSuperServiceImplClass("");
        strategyConfig.setSuperMapperClass(BaseMapper.class.getName());

        strategyConfig.setEntityLombokModel(true);    //开启lombook支持
        strategyConfig.setSuperMapperClass(com.baomidou.mybatisplus.core.mapper.BaseMapper.class.getName());
        return strategyConfig;
    }


    public static void main(String[] args) {
        AutoGenerator autoGenerator = new AutoGenerator();

        autoGenerator.setDataSource(dataSourceConfig());//把数据源配置添加到代码生成器主类
        autoGenerator.setGlobalConfig(globalConfig());//把全局配置添加到代码生成器主类
        autoGenerator.setPackageInfo(packageConfig());
        autoGenerator.setStrategy(strategyConfig());
        // 执行生成
        autoGenerator.execute();
    }
}
